package cn.bdqn;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTester {
    @Resource
    private RedisTemplate redisTemplate;
    @Test
    public void testString(){
        ValueOperations<String,Object> valueOperations=redisTemplate.opsForValue();
        //存储score数据
        valueOperations.set("score",59);
        //存储checkCode数据，并指定有效期
        valueOperations.set("checkCode","1234",30, TimeUnit.SECONDS);
        //获取score数据
        System.out.println(valueOperations.get("score"));
        System.out.println(redisTemplate.boundValueOps("checkCode").get());
        System.out.println("-------------------------------------");
        redisTemplate.delete("score");
        System.out.println(valueOperations.get("score"));
    }

    @Test
    public void testHash(){
        HashOperations<String,String,Object> hashOperations=redisTemplate.opsForHash();
        //存储数据到myhash里面
        hashOperations.put("myhash","name","李四");
        hashOperations.put("myhash","age",18);
        hashOperations.put("myhash","gender","男");

        //获取myhash里面指定的键值对
        Object name = hashOperations.get("myhash", "name");
        System.out.println(name);

        System.out.println("------------------");
        //修改myhash里面指定的键值对
        hashOperations.put("myhash","name","李四2");
        name = hashOperations.get("myhash", "name");
        System.out.println(name);
        System.out.println("----------------------------");

        //删除myhash里面指定键值对
        hashOperations.delete("myhash","name");
        name = hashOperations.get("myhash", "name");
        System.out.println(name);

        System.out.println("---------------------------");
        Map<String,Object> map= hashOperations.entries("myhash");
        for (String s : map.keySet()) {
            System.out.println(s);
        }

    }

    @Test
    public void testList(){
        ListOperations<String,Object> listOperations = redisTemplate.opsForList();
        //存储
        listOperations.leftPush("mylist","a");
        listOperations.leftPushAll("mylist","b","c");
        listOperations.rightPush("mylist","a");
        listOperations.rightPushAll("mylist","b","c");
        //获取mylist集合所有数据
        List<Object> list = listOperations.range("mylist", 0, -1);
        for (Object o  : list) {
            System.out.print(o+" ");
        }
        System.out.println();
        System.out.println("----------------");
        //删除
        Object leftPop = listOperations.leftPop("mylist");
        System.out.println(leftPop);
        System.out.println("---------------");
        //删除右边
        Object mylist = listOperations.rightPop("mylist");
        System.out.println(mylist);
        System.out.println("----------------");
        //获取mylist集合所有数据
        list = listOperations.range("mylist", 0, -1);
        for (Object o : list) {
            System.out.println(o+" ");
        }
    }
    @Test
    public void testSet(){
        SetOperations<String,Object>  setOperations = redisTemplate.opsForSet();
        setOperations.add("myset","a","a","b","c");
        //获取myset集合所有数据
        Set<Object> myset = setOperations.members("myset");
        for (Object o : myset) {
            System.out.print(o+" ");
        }
        System.out.println();
        System.out.println("----------------");
        //删除
        setOperations.remove("myset","a");

        //获取myset集合所有数据
        myset= setOperations.members("myset");
        for (Object o : myset) {
            System.out.print(o+" ");
        }
    }
    @Test
    public void testSortedset(){
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        //新增
        zSetOperations.add("mysortedset","张三",3);
        zSetOperations.add("mysortedset","李四",4);
        zSetOperations.add("mysortedset","王五",2);
        //遍历
        Set mysortedset = zSetOperations.range("mysortedset", 0, -1);
        for (Object ss : mysortedset) {
            System.out.print(ss+" ");
        }
        System.out.println();
        System.out.println("-----------------");
        //删除z
        zSetOperations.remove("mysortedset","张三");
        mysortedset = zSetOperations.range("mysortedset",0,-1);
        for (Object o : mysortedset) {
            System.out.print(o+" ");
        }

    }
}
